package com.lh0811.forge.pserv.uacs.server.adapter.controller;

import com.github.pagehelper.PageInfo;
import com.lh0811.forge.dependency.basic_web.constant.AuthConstant;
import com.lh0811.forge.dependency.basic_web.response.ServerResponse;
import com.lh0811.forge.dependency.security.client.component.annotation.HasUrl;
import com.lh0811.forge.pserv.uacs.api.component.data_scope.DataScope;
import com.lh0811.forge.pserv.uacs.api.param.UacsTenantDepartmentCreateParam;
import com.lh0811.forge.pserv.uacs.api.param.UacsTenantDepartmentQueryListParam;
import com.lh0811.forge.pserv.uacs.api.param.UacsTenantDepartmentQueryPageParam;
import com.lh0811.forge.pserv.uacs.api.param.UacsTenantDepartmentUpdateParam;
import com.lh0811.forge.pserv.uacs.api.vo.TDesignCascaderVo;
import com.lh0811.forge.pserv.uacs.api.vo.UacsTenantDepartmentQueryVo;
import com.lh0811.forge.pserv.uacs.server.service.TenantDepartmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "权限中心-租户组织机构管理")
@RestController
@RequestMapping("/forge/plate/v1/uacs/tenant_department")
public class OpmsTenantDepartmentController {

    @Resource
    private TenantDepartmentService tenantDepartmentService;


    // 创建组织机构信息
    @HasUrl
    @PutMapping("/create")
    @Operation(summary = "创建组织机构信息", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse tenantDepartmentCreate(@RequestBody @Valid UacsTenantDepartmentCreateParam param) throws Exception {
        tenantDepartmentService.tenantDepartmentCreate(param);
        return ServerResponse.createBySuccess();
    }

    // 删除组织机构信息
    @HasUrl
    @DeleteMapping("/delete/{tenantDepartmentId}")
    @Operation(summary = "删除组织机构信息", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse tenantDepartmentDelete(@PathVariable(value = "tenantDepartmentId") Long tenantDepartmentId) throws Exception {
        tenantDepartmentService.tenantDepartmentDelete(tenantDepartmentId);
        return ServerResponse.createBySuccess();
    }

    // 修改组织机构信息
    @HasUrl
    @PostMapping("/update")
    @Operation(summary = "修改组织机构信息", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    public ServerResponse tenantDepartmentUpdate(@RequestBody @Valid UacsTenantDepartmentUpdateParam param) throws Exception {
        tenantDepartmentService.tenantDepartmentUpdate(param);
        return ServerResponse.createBySuccess();
    }

    // 查询组织机构信息（分页）
    @HasUrl
    @GetMapping("/query/page")
    @Operation(summary = "查询组织机构信息（分页）", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    @DataScope
    public ServerResponse<PageInfo<UacsTenantDepartmentQueryVo>> tenantDepartmentQueryPage(@ParameterObject @Valid UacsTenantDepartmentQueryPageParam param) throws Exception {
        return ServerResponse.createBySuccess("查询成功", tenantDepartmentService.tenantDepartmentQueryPage(param));
    }

    // 查询组织机构信息（不分页）
    @HasUrl
    @GetMapping("/query/list")
    @Operation(summary = "查询组织机构信息（不分页）", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    @DataScope
    public ServerResponse<List<UacsTenantDepartmentQueryVo>> tenantDepartmentQueryList(@ParameterObject @Valid UacsTenantDepartmentQueryListParam param) throws Exception {
        return ServerResponse.createBySuccess("查询成功", tenantDepartmentService.tenantDepartmentQueryList(param));
    }

    // 查询级联选择列表
    @HasUrl
    @GetMapping("/query/cascader")
    @Operation(summary = "查询组织机构级联选择列表", parameters = {
            @Parameter(in = ParameterIn.HEADER, name = AuthConstant.TokenKey, description = "用户Token")
    })
    @DataScope
    public ServerResponse<List<TDesignCascaderVo>> tenantDepartmentQueryListCascader(@RequestParam(value = "tenantId", required = false) @PathVariable(value = "tenantId") Long tenantId) throws Exception {
        return ServerResponse.createBySuccess("查询成功", tenantDepartmentService.tenantDepartmentQueryListCascaderGroupByTenant(tenantId));
    }

}
